
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.15 Ajax & 跨域 · gitbook电子书</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Bom.html" />
    
    
    <link rel="prev" href="this指向.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../使用/使用.html">
            
                <a href="../使用/使用.html">
            
                    
                    md语法 & gitbook使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../面试题/面试题.html">
            
                <a href="../面试题/面试题.html">
            
                    
                    面试题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../面试题/html.html">
            
                <a href="../面试题/html.html">
            
                    
                    HTML
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../面试题/css.html">
            
                <a href="../面试题/css.html">
            
                    
                    CSS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../面试题/js.html">
            
                <a href="../面试题/js.html">
            
                    
                    JavaScript
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../面试题/vue.html">
            
                <a href="../面试题/vue.html">
            
                    
                    Vue
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../面试题/小程序.html">
            
                <a href="../面试题/小程序.html">
            
                    
                    小程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../面试题/编程题.html">
            
                <a href="../面试题/编程题.html">
            
                    
                    编程题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../html/">
            
                <a href="../html/">
            
                    
                    1. HTML
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../html/1.常用标签.html">
            
                <a href="../html/1.常用标签.html">
            
                    
                    1.1 常用标签
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../html/2.列表.html">
            
                <a href="../html/2.列表.html">
            
                    
                    1.2 列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../html/3.表单.html">
            
                <a href="../html/3.表单.html">
            
                    
                    1.3 表单属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../html/4.音频&视频.html">
            
                <a href="../html/4.音频&视频.html">
            
                    
                    1.4 ifame & audio & video
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../css/">
            
                <a href="../css/">
            
                    
                    2. CSS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../css/1.选择器.html">
            
                <a href="../css/1.选择器.html">
            
                    
                    2.1 选择器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../css/2.字体样式.html">
            
                <a href="../css/2.字体样式.html">
            
                    
                    2.2 字体样式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../css/3.背景.html">
            
                <a href="../css/3.背景.html">
            
                    
                    2.3 背景
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../css/4.盒子模型.html">
            
                <a href="../css/4.盒子模型.html">
            
                    
                    2.4 盒子模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../css/5.浮动.html">
            
                <a href="../css/5.浮动.html">
            
                    
                    2.5 浮动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../css/6.定位和渐变.html">
            
                <a href="../css/6.定位和渐变.html">
            
                    
                    2.6 定位 & 渐变
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../css/7.过渡&变形.html">
            
                <a href="../css/7.过渡&变形.html">
            
                    
                    2.7 过渡 & 变形
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../css/8.动画.html">
            
                <a href="../css/8.动画.html">
            
                    
                    2.8 动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../css/9.滤镜.html">
            
                <a href="../css/9.滤镜.html">
            
                    
                    2.9 滤镜
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../css/10.布局.html">
            
                <a href="../css/10.布局.html">
            
                    
                    2.10 布局
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    3. JS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="数据类型.html">
            
                <a href="数据类型.html">
            
                    
                    3.1 数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="语句.html">
            
                <a href="语句.html">
            
                    
                    操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="操作符.html">
            
                <a href="操作符.html">
            
                    
                    语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="基本类型和引用类型的值.html">
            
                <a href="基本类型和引用类型的值.html">
            
                    
                    基本类型和引用类型的值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="执行环境及作用域.html">
            
                <a href="执行环境及作用域.html">
            
                    
                    执行环境及作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="垃圾收集.html">
            
                <a href="垃圾收集.html">
            
                    
                    垃圾收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="Object类型.html">
            
                <a href="Object类型.html">
            
                    
                    Object类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.9" data-path="Array类型.html">
            
                <a href="Array类型.html">
            
                    
                    Array类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.10" data-path="Date类型.html">
            
                <a href="Date类型.html">
            
                    
                    Date类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.11" data-path="string&array.html">
            
                <a href="string&array.html">
            
                    
                    3.2 String & Array
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.12" data-path="date.html">
            
                <a href="date.html">
            
                    
                    3.3 Date类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.13" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    3.4 RegExp类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.14" data-path="global&math.html">
            
                <a href="global&math.html">
            
                    
                    3.5 Global对象 & Math对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.15" data-path="变量&作用域&垃圾收集.html">
            
                <a href="变量&作用域&垃圾收集.html">
            
                    
                    3.6 变量 & 作用域 & 垃圾收集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.16" data-path="流程控制&函数.html">
            
                <a href="流程控制&函数.html">
            
                    
                    3.7 流程控制 & 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.17" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    3.8 面向对象的程序设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.18" data-path="递归&冒泡&快速.html">
            
                <a href="递归&冒泡&快速.html">
            
                    
                    3.9 递归 & 冒泡 & 快速
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.19" data-path="定时器&防抖&节流.html">
            
                <a href="定时器&防抖&节流.html">
            
                    
                    3.10 定时器\防抖\节流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.20" data-path="DOM获取&DOM操作.html">
            
                <a href="DOM获取&DOM操作.html">
            
                    
                    3.11 DOM获取 & DOM操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.21" data-path="键盘事件&鼠标事件.html">
            
                <a href="键盘事件&鼠标事件.html">
            
                    
                    3.12 键盘事件 & 鼠标事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.22" data-path="事件冒泡&事件代理&事件绑定.html">
            
                <a href="事件冒泡&事件代理&事件绑定.html">
            
                    
                    3.13 事件冒泡\代理\绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.23" data-path="this指向.html">
            
                <a href="this指向.html">
            
                    
                    3.14 this指向
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.24" data-path="Ajax&跨域.html">
            
                <a href="Ajax&跨域.html">
            
                    
                    3.15 Ajax & 跨域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.25" data-path="Bom.html">
            
                <a href="Bom.html">
            
                    
                    3.16 BOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.26" data-path="ES6.html">
            
                <a href="ES6.html">
            
                    
                    3.17 ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.27" data-path="js实例.html">
            
                <a href="js实例.html">
            
                    
                    3.18 js实例
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    4. Vue
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vue/1.基础.html">
            
                <a href="../vue/1.基础.html">
            
                    
                    4.1 基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../vue/2.生命周期.html">
            
                <a href="../vue/2.生命周期.html">
            
                    
                    4.2 生命周期
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../vue/3.vuex.html">
            
                <a href="../vue/3.vuex.html">
            
                    
                    4.3 vuex
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../vue/4.原理.html">
            
                <a href="../vue/4.原理.html">
            
                    
                    4.4 深入响应式原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../vue/5.组件.html">
            
                <a href="../vue/5.组件.html">
            
                    
                    4.5 组件进阶
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../vue/6.规模化.html">
            
                <a href="../vue/6.规模化.html">
            
                    
                    4.6 规模化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../vue/7.过渡&动画.html">
            
                <a href="../vue/7.过渡&动画.html">
            
                    
                    4.7 过渡 & 动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../vue/8.可复用性&组合.html">
            
                <a href="../vue/8.可复用性&组合.html">
            
                    
                    4.8 可复用性 & 组合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../vue/9.工具.html">
            
                <a href="../vue/9.工具.html">
            
                    
                    4.9 工具
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../Node/">
            
                <a href="../Node/">
            
                    
                    5. Node
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../插件/">
            
                <a href="../插件/">
            
                    
                    插件
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.15 Ajax & 跨域</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="ajax">Ajax</h1>
<pre><code class="lang-javascript">    <span class="hljs-keyword">var</span> url_get = <span class="hljs-string">&quot;https://www.easy-mock.com/mock/5a91126f9d9af46407651597/test/test_get?num=10&quot;</span>;
    <span class="hljs-keyword">var</span> url_post = <span class="hljs-string">&quot;https://www.easy-mock.com/mock/5a91126f9d9af46407651597/test/test_post&quot;</span>;

    <span class="hljs-comment">//1.&#x521B;&#x5EFA;XML Http&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
    <span class="hljs-comment">//2.&#x8BBE;&#x7F6E;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</span>
    xhr.open(<span class="hljs-string">&quot;get&quot;</span>,url_get);
    <span class="hljs-comment">//3.&#x76D1;&#x542C;&#x72B6;&#x6001;&#x503C;&#x7684;&#x6539;&#x53D8;</span>
    xhr.addEventListener(<span class="hljs-string">&quot;readystatechange&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>)</span>{
        <span class="hljs-built_in">console</span>.log(e.target.readyState)
        <span class="hljs-comment">//&#x72B6;&#x6001;&#x503C; 4&#xFF1A;&#x4EE3;&#x8868;&#x6570;&#x636E;&#x89E3;&#x6790;&#x5B8C;&#x6210;</span>
        <span class="hljs-comment">//&#x72B6;&#x6001;&#x503C;200&#xFF1A;&#x8868;&#x793A;&#x8BF7;&#x6C42;&#x6210;&#x529F;</span>
        <span class="hljs-keyword">if</span>(e.target.readyState == <span class="hljs-number">4</span> &amp;&amp; e.target.status == <span class="hljs-number">200</span> ){
            <span class="hljs-built_in">console</span>.log(e.target.responseText)
            <span class="hljs-keyword">var</span> res = <span class="hljs-built_in">JSON</span>.parse(e.target.responseText)
            <span class="hljs-built_in">console</span>.log(res);
            <span class="hljs-comment">//&#x6E32;&#x67D3;&#x9875;&#x9762;&#x6216;&#x5904;&#x7406;&#x6570;&#x636E;</span>
            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i <span class="hljs-keyword">in</span> res.data.member ){
                <span class="hljs-built_in">document</span>.write(res.data.member[i])
            }
        }
    })
    <span class="hljs-comment">//4.&#x53D1;&#x9001; post&#x8BF7;&#x6C42;&#xFF1A;xhr.send(&quot;name=haha&amp;age=12&quot;)</span>
    xhr.send();
</code></pre>
<blockquote>
<p>&#x524D;&#x540E;&#x7AEF;&#x5DE5;&#x4F5C;&#x4EA4;&#x63A5;&#xFF0C;&#x5982;&#x679C;&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8981;&#x4ECE;&#x96F6;&#x5F00;&#x53D1;&#xFF0C;&#xFF0C;&#x5148;&#x5B9A;&#x597D;&#x63A5;&#x53E3;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x548C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x5199;&#x597D;&#x6587;&#x6863;&#x5217;&#x660E;&#x524D;&#x7AEF;&#x63D0;&#x4EA4;&#x7684;&#x53C2;&#x6570; &#x548C; &#x540E;&#x7AEF;&#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E;&#xFF1B;&#x63A5;&#x5165;&#x5230;&#x6B63;&#x5F0F;&#x6570;&#x636E;&#xFF0C;&#x6700;&#x597D;&#x5728;&#x6D4B;&#x8BD5;&#x73AF;&#x5883;&#x4E0B;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;&#xFF0C;&#x4E0A;&#x7EBF;&#x65F6;&#x95F4;&#x5C3D;&#x53EF;&#x80FD;&#x907F;&#x5F00;&#x9AD8;&#x5CF0;&#x671F;&#xFF08;&#x4E0A;&#x4E0B;&#x73ED;&#x65F6;&#x95F4;&#x3001;&#x5468;&#x672B;&#x65F6;&#x95F4;&#xFF09;&#xFF1B;</p>
</blockquote>
<pre><code class="lang-js">    <span class="hljs-keyword">var</span> baseUrl = <span class="hljs-string">&quot;https://www.easy-mock.com/mock/5bed3e460dfee6695b4bc7ae/text20&quot;</span>
    <span class="hljs-keyword">var</span> test_get  = baseUrl + <span class="hljs-string">&quot;/mytest&quot;</span>; <span class="hljs-comment">//&#x57FA;&#x7840;&#x8DEF;&#x5F84;+&#x63A5;&#x53E3;&#x5730;&#x5740;</span>

    ajax_get(test_get,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>)</span>{
        <span class="hljs-keyword">if</span>(e.target.readyState == <span class="hljs-number">4</span> &amp;&amp; e.target.status == <span class="hljs-number">200</span>){
                <span class="hljs-keyword">var</span> res = <span class="hljs-built_in">JSON</span>.parse(e.target.responseText)
                <span class="hljs-built_in">console</span>.log(res);
            }
    })
    <span class="hljs-comment">// &#x5C01;&#x88C5;&#x529F;&#x80FD; </span>
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ajax_get</span>(<span class="hljs-params">url,callback</span>)</span>{
        <span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
        xhr.open(<span class="hljs-string">&quot;get&quot;</span>,url);
        xhr.addEventListener(<span class="hljs-string">&quot;readystatechange&quot;</span>,callback)
        xhr.send();
    }
</code></pre>
<pre><code>//&#x5047;&#x8BBE; &#x63A5;&#x53E3;&#x5730;&#x5740;&#xFF1A;http&#xFF1A;//www.easy-mock.com/test
/**
 * get &#x548C; post&#x7684;&#x533A;&#x522B;
 * 1.get &#x53C2;&#x6570; &#x62FC;&#x63A5;&#x5728;url&#x540E;&#x9762; &#x4F8B;&#x5982;url + &quot;?name=xx&amp;&amp;age=12 &quot;
 * 2.post &#x53C2;&#x6570; &#x653E;&#x5728;&#x8BF7;&#x6C42;&#x4F53;&#xFF08;body&#xFF09;
 * 
 * &#x4F20;&#x53C2;&#x7684;&#x65B9;&#x5F0F;&#x4E0D;&#x540C;&#xFF0C;get&#x8BF7;&#x6C42;&#x4F1A;&#x53D7;&#x5230;url&#x6700;&#x5927;&#x957F;&#x5EA6;&#x63A7;&#x5236;&#xFF0C;post&#x7684;&#x4FE1;&#x606F;&#x91CF;&#x6BD4;get&#x8981;&#x5927;
 * 
 * &#x76F8;&#x5BF9;&#x800C;&#x8A00;&#xFF0C;post&#x8BF7;&#x6C42;&#x4F1A;&#x6BD4;&#x8F83;&#x5B89;&#x5168;&#x4E9B;
 * **/
</code></pre><pre><code class="lang-javascript">    <span class="hljs-keyword">var</span> url_post = <span class="hljs-string">&quot;https://www.easy-mock.com/mock/5bed3e460dfee6695b4bc7ae/text20/text_post&quot;</span>
    <span class="hljs-comment">// &#x521B;&#x5EFA;XML HTTP&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">var</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
    <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</span>
    xhr.open(<span class="hljs-string">&quot;post&quot;</span>,url_post);
    <span class="hljs-comment">// &#x53D1;&#x9001;&#x5408;&#x9002;&#x7684;&#x8BF7;&#x6C42;&#x5934;&#x4FE1;&#x606F;</span>
    xhr.setRequestHeader(<span class="hljs-string">&quot;Content-type&quot;</span>,<span class="hljs-string">&quot;application/x-www-form-ulencoded&quot;</span>);

    xhr.addEventListener(<span class="hljs-string">&quot;readystatechange&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>)</span>{
        <span class="hljs-keyword">if</span>(e.target.readyState==<span class="hljs-number">4</span>&amp;&amp;e.target.status==<span class="hljs-number">200</span>){
            <span class="hljs-keyword">var</span> res = <span class="hljs-built_in">JSON</span>.parse(e.target.responseText);
            <span class="hljs-built_in">console</span>.log(res)
            render(res);
        }
    })
    xhr.send(<span class="hljs-string">&quot;name=haha&quot;</span>)

    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">render</span>(<span class="hljs-params">result</span>)</span>{
        <span class="hljs-comment">//&#x6E32;&#x67D3;&#x5904;&#x7406;</span>
        <span class="hljs-built_in">console</span>.log(result)
    }

    <span class="hljs-keyword">if</span>(XMLHttpRequest){
        xhr2 = <span class="hljs-keyword">new</span> XMLHttpRequest()
    }<span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(<span class="hljs-built_in">window</span>.ActiveXObject){
        <span class="hljs-comment">//ie6 ie5</span>
        xhr2 = <span class="hljs-keyword">new</span> ActiveXObject(<span class="hljs-string">&quot;Msxml2.XMLHTTP&quot;</span>)
    }
</code></pre>
<h1 id="&#x8DE8;&#x57DF;">&#x8DE8;&#x57DF;</h1>
<pre><code class="lang-js"><span class="hljs-comment">//&#x57DF;&#x540D;&#xFF1A;&#x534F;&#x8BAE;(http:)//&#x4E8C;&#x7EA7;&#x57DF;&#x540D;.&#x4E00;&#x7EA7;&#x57DF;&#x540D;:&#x7AEF;&#x53E3;&#x53F7;</span>
<span class="hljs-comment">//&#x53EA;&#x8981;&#x4EE5;&#x4E0A;&#x6709;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x4E0D;&#x76F8;&#x540C;&#xFF0C;&#x90FD;&#x53EF;&#x4EE5;&#x79F0;&#x4E4B;&#x4E3A;&#x8DE8;&#x57DF;</span>

<span class="hljs-comment">//&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x5B89;&#x5168;&#x7B56;&#x7565;&#xFF1A;&#x540C;&#x6E90;&#x7B56;&#x7565;</span>
<span class="hljs-comment">/**
 * jsonp
 * &#x5C06;&#x6570;&#x636E;&#x653E;&#x5728;&#x524D;&#x7AEF;&#x8BF7;&#x6C42;&#x65F6;&#x8BBE;&#x7F6E;&#x7684;&#x56DE;&#x8C03;&#x540D;&#x91CC;&#x9762;&#xFF0C;&#x7ECF;&#x8FC7;&#x51FD;&#x6570;&#x540D;&#x7684;&#x5305;&#x88F9;&#x8FD4;&#x56DE;
 * &#x4F8B;&#x5982;&#xFF1A;http://127.0.0.1:3000/jsonp?callback=qwer
 * &#x8FD4;&#x56DE;&#xFF1A;qwer&#xFF08;{data&#xFF1A;123}&#xFF09;
 * **/</span>

<span class="hljs-comment">/**
 * &#x8DE8;&#x57DF;&#x7684;&#x65B9;&#x5F0F;&#xFF1A;
 * 1.jsonp &#x53EA;&#x80FD;&#x662F;get &#x524D;&#x7AEF;&#x52A8;&#x6001;&#x6DFB;&#x52A0;script &#x540E;&#x7AEF;&#x4E5F;&#x8981;&#x54CD;&#x5E94;&#x5904;&#x7406;
 * 2.&#x540E;&#x7AEF;&#x6253;&#x5F00;Access-Control-Allow-Origin&#xFF1A;*
 * 3.&#x53CD;&#x5411;&#x4EE3;&#x7406;&#xFF1A;&#x8BA9;&#x81EA;&#x5DF1;&#x540E;&#x7AEF;&#x670D;&#x52A1;&#x5668;&#x8BF7;&#x6C42;&#x522B;&#x4EBA;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x81EA;&#x5DF1;&#x670D;&#x52A1;&#x5668;&#x62FF;&#x5230;&#x6570;&#x636E;&#x4E4B;&#x540E;&#x518D;&#x7ED9;&#x56DE;&#x524D;&#x7AEF;
 * **/</span>
<span class="hljs-keyword">var</span> url_jsonp = <span class="hljs-string">&quot;http://suggest.taobao.com/sug?code=utf-8&amp;q=&#x9F20;&#x6807;&amp;callback=cbbb&quot;</span>;
<span class="hljs-comment">//&#x521B;&#x5EFA;script&#x6807;&#x7B7E;</span>
<span class="hljs-keyword">var</span> script = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&quot;script&quot;</span>);
<span class="hljs-comment">//&#x52A0;&#x8F7D;&#x8D44;&#x6E90;&#xFF08;&#x63A5;&#x53E3;&#xFF09;</span>
script.src=url_jsonp;
<span class="hljs-comment">//&#x6DFB;&#x52A0;&#x5230;&#x9875;&#x9762;</span>
<span class="hljs-built_in">document</span>.body.appendChild(script);
<span class="hljs-comment">//&#x56DE;&#x8C03;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cbbb</span>(<span class="hljs-params">e</span>)</span>{
    <span class="hljs-built_in">console</span>.log(e);
}
<span class="hljs-comment">//&#x8FD4;&#x56DE; &#x7684;&#x662F;&#x4E00;&#x4E32;&#x4EE3;&#x7801; cbbb&#xFF08;&#x6570;&#x636E;&#xFF09;</span>
cbbb({result:[[<span class="hljs-string">&quot;&#x9F20;&#x6807;&#x57AB;&quot;</span>,<span class="hljs-string">&quot;771666&quot;</span>]]})



<span class="hljs-comment">//&#x5F53;&#x524D;    http://127.0.0.1:59237</span>
<span class="hljs-comment">//&#x7EBF;&#x4E0A;&#x63A5;&#x53E3; http://m.maizuo.com</span>


<span class="hljs-comment">//            ajax_get()</span>

<span class="hljs-comment">//            ajax_get(test_get,function(e){</span>
<span class="hljs-comment">//                if(e.target.readyState == 4 &amp;&amp; e.target.status == 200){</span>
<span class="hljs-comment">//                        var res = JSON.parse(e.target.responseText)</span>
<span class="hljs-comment">//                        console.log(res);</span>
<span class="hljs-comment">//                    }</span>
<span class="hljs-comment">//            })</span>
<span class="hljs-comment">//            </span>
<span class="hljs-comment">//            function ajax_get(url,callback){</span>
<span class="hljs-comment">//                var xhr = new XMLHttpRequest();</span>
<span class="hljs-comment">//                xhr.open(&quot;get&quot;,url);</span>
<span class="hljs-comment">//                xhr.addEventListener(&quot;readystatechange&quot;,callback)</span>
<span class="hljs-comment">//                xhr.send();</span>
<span class="hljs-comment">//            }</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="this指向.html" class="navigation navigation-prev " aria-label="Previous page: 3.14 this指向">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Bom.html" class="navigation navigation-next " aria-label="Next page: 3.16 BOM">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.15 Ajax & 跨域","level":"1.5.24","depth":2,"next":{"title":"3.16 BOM","level":"1.5.25","depth":2,"path":"JS/Bom.md","ref":"JS/Bom.md","articles":[]},"previous":{"title":"3.14 this指向","level":"1.5.23","depth":2,"path":"JS/this指向.md","ref":"JS/this指向.md","articles":[]},"dir":"ltr"},"config":{"plugins":["toggle-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"toggle-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"gitbook电子书","gitbook":"*","description":"记录前端相关的知识点以及一些示例的demo"},"file":{"path":"JS/Ajax&跨域.md","mtime":"2019-03-19T05:59:39.481Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-09-06T08:37:43.293Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

